//
//  LanguageTableViewDataSource.m
//  LanguagesTeach
//
//  Created by Niel Santos on 2/9/13.
//  Copyright 2013 __MyCompanyName__. All rights reserved.
//

#import "LanguageTableViewDataSource.h"

#import "Level.h"
#import "MetaUnit.h"
#import "RowTableCell.h"


@implementation LanguageTableViewDataSource

@synthesize subjectPath;

- (Level*)getLevelByLevelFolder: (NSString*) levelFolder{
	Level *level;
	
	for(int i=0;i < [xmlLanguageParser.levels count]; i++){
		level = [xmlLanguageParser.levels objectAtIndex: i];
		if ([[level folder] isEqualToString:levelFolder]){
			return level;
		}
	}
	return nil;
}

- (id)init: (SubjectPath*) localSubjectPath {
	
	NSString *fileDirectory;
	NSString *filePath;
	NSURL *url;
	NSXMLParser *xmlParser;
	BOOL success;
	
    if (self = [super init])
    {	
		subjectPath = localSubjectPath;
		fileDirectory = [NSString stringWithFormat:@"data/%@", [localSubjectPath languagePrefix]];
		filePath = [[NSBundle mainBundle] pathForResource:@"language" ofType:@"xml" inDirectory: fileDirectory];
		url = [NSURL fileURLWithPath:filePath];
		
	  
		xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
		xmlLanguageParser = [[XMLLanguageParser alloc] init];
		xmlParser.delegate = xmlLanguageParser;
		success = [xmlParser parse];
		
		
		[xmlParser release];
		
		if(success){
			NSLog(@"No errors found");
		}else{
			NSLog(@"Warning a error was found");
		}
    }
    return self;	
}


#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSString *levelFolder;
	Level *level;
	NSMutableArray *metaUnits;
	
	
	
	levelFolder = [subjectPath levelFolder];
	NSLog(@"level_folder:%@",levelFolder);
	if(levelFolder != nil){
		level = [self getLevelByLevelFolder: levelFolder];
		metaUnits = [level metaUnits];
		return [metaUnits count];
	}else {
		return [xmlLanguageParser.levels count];
	}

	
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Level *level;
	NSString *levelFolder;
	NSMutableArray *metaUnits;
	MetaUnit *unit;
	
    static NSString *CellIdentifier = @"Cell";
    
    RowTableCell *cell = (RowTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[RowTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
		//accessory type
		cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;			
    }
	levelFolder = [subjectPath levelFolder];
    if(levelFolder == nil){
		// Configure the cell.
		level = [xmlLanguageParser.levels objectAtIndex:indexPath.row];
		[cell.textLabel setText: [level title]];
		cell.prefix = level.folder;
		
	} else {
		level = [self getLevelByLevelFolder: levelFolder];
		metaUnits = [level metaUnits];
		unit = [metaUnits objectAtIndex:indexPath.row];
		[cell.textLabel setText: [unit title]];
		cell.prefix = unit.subFolder;
	}
	return cell;
}


- (void)dealloc {
	[xmlLanguageParser release];
    [super dealloc];
}

@end
